﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using SyncList.Business.ValueObject;
using SyncList.Business.Helpers;

namespace SyncList.Business.Services
{
    class ExportService : IExportService
    {

        /// <summary>
        /// Exporter en XML la liste des fichies scannés
        /// </summary>
        /// <param name="data">Données à sauver</param>
        /// <param name="fileName">Nom du fichier cible</param>
        /// <param name="minSize">Taille mini</param>
        public ExportInfo ExportList(Data data, string fileName, long minSize)
        {
            ExportInfo result = new ExportInfo();

            if(File.Exists(fileName))
                File.Copy(fileName, fileName + ".beforeexport.bak", true);

            XDocument xDoc = new XDocument();            
            XElement rootElement = new XElement("SyncList");
            rootElement.SetAttributeValue("type", "scan");
            xDoc.Add(rootElement);

            // - Paramètres
            XElement definitionElement = new XElement("Definition");
            rootElement.Add(definitionElement);

            // - Utilisateur
            XElement userNameElement = new XElement("UserName");
            userNameElement.SetValue(data.UserName);
            definitionElement.Add(userNameElement);

            // - Titre
            XElement titleElement = new XElement("Title");
            titleElement.SetValue(data.Title);
            definitionElement.Add(titleElement);

            // - Liste des chemins 
            XElement pathListElement = new XElement("PathList");
            definitionElement.Add(pathListElement);
            foreach (string path in data.Folders)
            {
                XElement pathElement = new XElement("Path");
                pathElement.SetValue(path);
                pathListElement.Add(pathElement);
            }

            // - Taille mini
            XElement filterElement = new XElement("Filter");
            filterElement.SetAttributeValue("MinSize", data.FileFilter.MinSize);
            definitionElement.Add(filterElement);


            // --------- Totaux            
            XElement infoElement = new XElement("Info");
            rootElement.Add(infoElement);
            

            DateTime exportDate =  DateTime.Now;
            string exportDateCatpion = exportDate.ToString("yyyy-MM-dd HH:mm:ss");

            infoElement.SetAttributeValue("Date", exportDateCatpion);

         



            XElement filesElement = new XElement("Files");
            rootElement.Add(filesElement);

            int index = 0;
            long totalSize = 0;
            int fileCount = 0;

            foreach (FileItem def in data.Files.OrderBy(o => o.Name))
            {
                if (def.Size < minSize) continue; // Pas moins de 10 Mo
                if (def.Name.Equals("Thumbs.db")) continue;

                XElement fileElement = new XElement("File");
                fileElement.SetAttributeValue("num", index+1);
                fileElement.SetAttributeValue("name", def.Name);
                fileElement.SetAttributeValue("size", def.Size);
                fileElement.SetAttributeValue("sizecaption", SizeHelper.FormatSize(def.Size));
                fileElement.SetAttributeValue("hash", def.Hash);
                fileElement.SetAttributeValue("folder", def.LocalFolder);
                filesElement.Add(fileElement);

                totalSize += def.Size;
                fileCount++;

                index++;
            }


            string totalSizeCaption = SizeHelper.FormatSize(totalSize);

            infoElement.SetAttributeValue("FileCount", fileCount);
            infoElement.SetAttributeValue("TotalSize", totalSize);
            infoElement.SetAttributeValue("TotalSizeCaption", totalSizeCaption);



            xDoc.Save(fileName);

            result.FileCount = fileCount;
            result.TotalSizeCaption = totalSizeCaption;
            result.dateTimeCaption = exportDateCatpion;
            return(result);
        }




        /// <summary>
        /// Exporter en XML la liste de ce que l'on désire
        /// </summary>
        /// <param name="root">Liste des fichiers demandés</param>
        /// <param name="fileName">Nom du fichier de sauvegarde</param>
        public void ExportWishes(DirectoryContent root, string fileName)
        {
            XDocument xDoc = new XDocument();
            XElement rootElement = new XElement("SyncList");
            rootElement.SetAttributeValue("type", "wishes");
            xDoc.Add(rootElement);


            // - Users
            XElement usersElement = new XElement("Users");
            rootElement.Add(usersElement);


            // - Liste des Users
            foreach(DirectoryContent userDirectory in root.Directories.Values)
            {
                XElement userElement = new XElement("User");
                userElement.SetAttributeValue("name", userDirectory.Name);
                usersElement.Add(userElement);

                XElement infoElement = new XElement("Info");
                userElement.Add(infoElement);


                long totalSize = 0;
                int fileCount = 0;

                XElement filesElement = new XElement("Files");
                userElement.Add(filesElement);

                if (userDirectory.DirectoryType == DirectoryTypeEnum.User)
                {
                    foreach(FileItem file in userDirectory.AllFiles)
                    {
                        // - Un fichier
                        XElement fileElement = new XElement("File");
                        fileElement.SetAttributeValue("folder", file.LocalFolder);
                        fileElement.SetAttributeValue("name", file.Name);
                        fileElement.SetAttributeValue("size", file.Size);
                        fileElement.SetAttributeValue("sizecaption", SizeHelper.FormatSize(file.Size));
                        fileElement.SetAttributeValue("hash", file.Hash);

                        filesElement.Add(fileElement);

                        fileCount++;
                        totalSize += file.Size;
                    }
                }

                DateTime exportDate = DateTime.Now;
                string exportDateCatpion = exportDate.ToString("yyyy-MM-dd HH:mm:ss");
                infoElement.SetAttributeValue("Date", exportDateCatpion);
                infoElement.SetAttributeValue("FileCount", fileCount);
                infoElement.SetAttributeValue("TotalSize", totalSize);
                infoElement.SetAttributeValue("TotalSizeCaption", SizeHelper.FormatSize(totalSize));                
            }

            xDoc.Save(fileName);       
        }

    }
}
